package mts;

import java.io.File;
import java.io.IOException;

import mts.common.CommonSettings;

import org.apache.log4j.Logger;

public class ElevatorControllerManager {

	private static final Logger log = Logger.getLogger(ElevatorControllerManager.class);
	
	private static Process[] elevatorControllerProcesses = new Process[CommonSettings.MAX_ELEVATOR_CONTROLLERS];
	
	public static void startElevatorProcess(byte carNum) {
		log.error(new File(".").getAbsolutePath());
		try {
			elevatorControllerProcesses[carNum] = Runtime.getRuntime().exec(new String[] {".././elevator controller/EClient", Byte.toString(carNum)});
			
		} catch (IOException ex) {
			log.error("Couldn't start elevator controller process", ex);
		}
		
		try {
			Thread.sleep(400);
		} catch (InterruptedException e) {
			log.error("Could not sleep", e);
		}
		
	}
	
	public static void stopElevatorControllerProcess(byte carNum) {
		elevatorControllerProcesses[carNum].destroy();
	}
	
	public static void stopAllElevatorControllerProcesses() {
		for (byte i = 0; i < CommonSettings.MAX_ELEVATOR_CONTROLLERS; i++) {
			if (elevatorControllerProcesses[i] == null) {
				continue;
			}
			
			stopElevatorControllerProcess(i);
		}
	}
	
}
